#include <iostream>
#include <vector>

using namespace std;

class Person
{
public:
    Person(string name, int age)
    {
        m_age = age;
        m_name = name;
    }

    string m_name;
    int m_age;
};

void test1()
{
    vector<Person> v;

    Person p1("aa", 1);
    Person p2("bb", 2);

    v.push_back(p1);
    v.push_back(p2);

    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << it->m_name << endl;
    }
    cout << v[1].m_name << endl;
}

// template <class T>
void printVector(vector<int> &v)
{
    for (vector<int>::iterator i = v.begin(); i != v.end(); ++i)
    {
        cout << *i << " ";
    }
    cout << endl;
}

void test2()
{
    vector<int> v1;
    for (int i = 0; i < 10; ++i)
    {
        v1.push_back(i);
    }
    printVector(v1); //0 1 2 3 4 5 6 7 8 9
    vector<int> v2(10, 22);
    printVector(v2); //22 22 22 22 22 22 22 22 22 22

    vector<int> v3(v1.begin(), v1.end());
    printVector(v3); //0 1 2 3 4 5 6 7 8 9
    v3 = v2;
    printVector(v3); //22 22 22 22 22 22 22 22 22 22

    vector<int> v4(v1);
    printVector(v4); //0 1 2 3 4 5 6 7 8 9
    v4.assign(2, 5); // 2个5
    printVector(v4);
    v4.assign(v1.begin(), v1.end());
    printVector(v4);
    v4.resize(30, 999); // 扩展，多出来的补999
    vector<int>::iterator it;
    it = v4.begin();
    it = v4.insert(it, 33); // 在某个位置插入,插完了it不变
    cout << *it << endl;
    printVector(v4);
    v4.insert(it, 2, 100); //插2个100

    printVector(v4);

    v4.erase(v4.begin()); // 删除
    v4.erase(v4.begin(), v4.begin() + 3);
    printVector(v4);
    cout << v4.front() << " " << v4.back() << endl;

    v4.clear();

    cout << v4.size() << " " << v4.capacity() << endl;

    vector<int> (v4).swap(v4);  // 收缩内存
cout << v4.size() << " " << v4.capacity() << endl;

v4.reserve(2000);  // 预留空间
cout << v4.size() << " " << v4.capacity() << endl;

}

int main()
{
    test1();
    test2();
}